package org.vaadin.pekka.distributionbartable.widget.client.ui;

import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.dom.client.Style.VerticalAlign;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Label;
import com.vaadin.terminal.gwt.client.BrowserInfo;

public class DistributionBarTableElementBuilder {

	public Element createDistributionBarToRow(DistributionBarTableData state,
			int row) {
		final DivElement bar = Document.get().createDivElement();
		if (BrowserInfo.get().isIE6())
			bar.setClassName(VDistributionBarTable.CLASSNAME + "-bar-ie6");
		else
			bar.setClassName(VDistributionBarTable.CLASSNAME + "-bar");
		return bar;
	}

	public Element createBarSectionElement() {
		final Element section = Document.get().createDivElement();
		// IE6 doesn't support the " > " child selector in css..
		if (BrowserInfo.get().isIE6()) {
			section.getStyle().setDisplay(Display.INLINE);
			section.getStyle().setFloat(Style.Float.LEFT);
			section.getStyle().setOverflow(Overflow.HIDDEN);
			section.getStyle().setVerticalAlign(VerticalAlign.MIDDLE);
		}
		section.appendChild(createSectionCaption(""));
		return section;
	}

	public Element updateSection(final Element section,
			final Element tableCell, final DistributionBarTableData state,
			int row, int sectionIndex) {
		section.setClassName(VDistributionBarTable.CLASSNAME + "-section"
				+ sectionIndex);
		section.setTitle(SafeHtmlUtils.htmlEscape(state.getDescriptions().get(
				row)[sectionIndex]));
		if (state.getColorMode() != 0) {
			final String[] colors = state.getColorsCSSNames(sectionIndex);
			section.getStyle().setBackgroundColor(colors[0]);
			section.getStyle().setColor(colors[1]);
		}
		section.getFirstChildElement().setInnerText(
				state.getSectionCaptions().get(row)[sectionIndex]);
		section.getStyle().setWidth(
				state.getDistributionPixels(tableCell, row, sectionIndex),
				Unit.PX);
		return section;
	}

	public Label createCaptionLabel(String caption, String type) {
		final Label l = new Label(caption);
		DOM.setElementProperty(l.getElement(), "className",
				VDistributionBarTable.CLASSNAME + "-" + type);
		return l;
	}

	public Element createSectionCaption(String caption) {
		final Element e = Document.get().createSpanElement();
		e.setClassName(VDistributionBarTable.CLASSNAME + "-bar-caption");
		e.setInnerText(caption);
		return e;
	}

	public Element createTableHeaderBar(final DistributionBarTableData state) {
		Element headerBar = Document.get().createDivElement();
		headerBar.setClassName(VDistributionBarTable.CLASSNAME + " -headerbar");
		for (int sectionIndex = 0; sectionIndex < state.getSections(); sectionIndex++) {
			Element headerSection = Document.get().createSpanElement();
			if (state.getColorMode() == 0) {
				headerSection.setClassName(VDistributionBarTable.CLASSNAME
						+ "-section" + sectionIndex);
			} else {
				final String[] colors = state.getColorsCSSNames(sectionIndex);
				headerSection.getStyle().setBackgroundColor(colors[0]);
				headerSection.getStyle().setColor(colors[1]);
			}
			headerSection
					.setInnerText(state.getDescriptions().get(0)[sectionIndex]);
			headerSection.getStyle().setWidth((100.0 / state.getSections()),
					Unit.PCT);
		}
		return headerBar;
	}
}
